<template>
  <el-row class="home" :gutter="20">
    <el-col :span="8">
      <el-card shadow="hover">
        <div class="user">
          <img :src="userImg" />
          <div class="userinfo">
            <p class="name">Nick</p>
            <p class="access">超级管理员</p>
          </div>
        </div>
        <div class="login-info">
          <p>上次登录时间：<span>2021-08-15</span></p>
          <p>上次登录地点：<span>郑州</span></p>
        </div>
      </el-card>
      <el-card shadow="hover" style="height: 522px; margin-top: 20px">
        <el-table :data="tableData" border>
          <el-table-column show-overflow-tooltip v-for="(val, key) in tableLabel" :key="key" :prop="key" :label="val" />
        </el-table>
      </el-card>
    </el-col>
    <el-col :span="16">
      <div class="num">
        <el-card shadow="hover" v-for="item in countData" :key="item.name" :body-style="{ display: 'flex', padding: 0 }">
          <i class="icon" :class="`el-icon-${item.icon}`" :style="{ background: item.color }"></i>
          <div class="detail">
            <p class="num">￥{{ item.value }}</p>
            <p class="txt">{{ item.name }}</p>
          </div>
        </el-card>
      </div>
      <el-card shadow="hover">
        <Echart style="height: 280px" :chartData="echartData.order"></Echart>
      </el-card>
      <div class="graph">
        <el-card shadow="hover">
          <Echart style="height: 260px" :chartData="echartData.user"></Echart>
        </el-card>
        <el-card shadow="hover">
          <Echart style="height: 260px" :chartData="echartData.video" :isAxisChart="false"></Echart>
        </el-card>
      </div>
    </el-col>
  </el-row>
</template>

<script>
import Echart from '@/components/Echart'
export default {
  components: {
    Echart,
  },
  mounted() {
    this.getHomeData()
  },
  data() {
    return {
      userImg: require('../../assets/images/user.png'),
      countData: [
        {
          name: '今日支付订单',
          value: 1234,
          icon: 'success',
          color: '#2ec7c9',
        },
        {
          name: '今日收藏订单',
          value: 210,
          icon: 'star-on',
          color: '#ffb980',
        },
        {
          name: '今日未支付订单',
          value: 1234,
          icon: 's-goods',
          color: '#5ab1ef',
        },
        {
          name: '本月支付订单',
          value: 1234,
          icon: 'success',
          color: '#2ec7c9',
        },
        {
          name: '本月收藏订单',
          value: 210,
          icon: 'star-on',
          color: '#ffb980',
        },
        {
          name: '本月未支付订单',
          value: 1234,
          icon: 's-goods',
          color: '#5ab1ef',
        },
      ],
      tableData: [],
      tableLabel: {
        name: '课程',
        todayBuy: '今日购买',
        monthBuy: '本月购买',
        totalBuy: '总购买',
      },
      echartData: {
        // 在order中这样定义数据的好处是可以在开发工具中提示，如果通过order.xData=[] 这种方式工具中是点不出来的
        order: {
          xData: [],
          series: [],
        },
        user: {
          xData: [],
          series: [],
        },
        video: {
          series: [],
        },
      },
    }
  },
  methods: {
    getHomeData() {
      this.$http.get('/home/getHomeData').then((res) => {
        res = res.data
        this.tableData = res.data.tableData
        // 折线图
        // console.log(res)
        // 拿到折线图数据
        const order = res.data.orderData
        this.echartData.order.xData = order.date
        // 通过Object.keys(order.data[0])可以获得数据中所有的是键
        let keyArray = Object.keys(order.data[0])
        // console.log(keyArray)
        // 循环键根据键添加serides系列
        keyArray.forEach((key) => {
          this.echartData.order.series.push({
            // 因为后台返回的数据wchart就是微信小程序 所以可以如下处理
            name: key === 'WeChart' ? '小程序' : key,
            type: 'line',
            // 可以通过map方法返回一个数组，根据key可以从数据中取出对应的数据并放在一个数组中返回，处理这种数据推荐使用
            data: order.data.map((item) => item[key]),
          })
        })
        // 获取用户数据
        const user = res.data.userData
        // console.log(user)
        this.echartData.user.xData = user.map((item) => item.date)
        this.echartData.user.series.push({
          name: '新增用户',
          data: user.map((item) => item.new),
          type: 'bar',
        })
        this.echartData.user.series.push({
          name: '活跃用户',
          data: user.map((item) => item.active),
          type: 'bar',
          barGap: 0,
        })
        //饼图
        const video = res.data.videoData
        // console.log(video)
        this.echartData.video.series.push({
          type: 'pie',
          data: video,
        })
      })
    },
  },
}
</script>

<style lang="scss" scoped>
@import '~@/assets/scss/home.scss';
</style>
